Skip to content

Latest commit

 

History

History
43 lines (36 loc) · 1.16 KB

File metadata and controls

43 lines (36 loc) · 1.16 KB

718. Maximum Length of Repeated Subarray

Given two integer arrays nums1 and nums2, return the maximum length of a subarray that appears in both arrays.

Example 1:

Input: nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7] Output: 3 Explanation: The repeated subarray with maximum length is [3,2,1]. 

Example 2:

Input: nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0] Output: 5 Explanation: The repeated subarray with maximum length is [0,0,0,0,0]. 

Constraints:

  • 1 <= nums1.length, nums2.length <= 1000
  • 0 <= nums1[i], nums2[i] <= 100

Solutions (Rust)

1. Solution

implSolution{pubfnfind_length(nums1:Vec<i32>,nums2:Vec<i32>) -> i32{letmut dp = vec![vec![0; nums2.len() + 1]; nums1.len() + 1];letmut ret = 0;for i in0..nums1.len(){for j in0..nums2.len(){if nums1[i] == nums2[j]{ dp[i + 1][j + 1] = dp[i][j] + 1; ret = ret.max(dp[i + 1][j + 1]);}}} ret }}
close